package com.lg.system.wxgroup.controller;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.lg.system.menu.entity.Menu;
import com.lg.system.wxgroup.entity.WxGroup;
import com.lg.system.wxgroup.service.WxGroupService;
import com.lg.utils.PageModel;
import com.lg.utils.Query;
import com.lg.utils.R;
import com.lg.utils.Tree;
import com.lg.wechat.common.base.BaseController;

import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.tag.WxUserTag;

/**
 * <pre>
 *  注意：此contorller 实现WxMpMenuService接口，仅是为了演示如何调用所有菜单相关操作接口，
 *      实际项目中无需这样，根据自己需要添加对应接口即可
 * </pre>
 *
 * @author dl
 */
@RestController
@RequestMapping("/wxgroup")
public class WxGroupController extends BaseController {

  private String prefix="system/wxgroup/";
  
  @Autowired
  private WxMpService wxMpService;
  
  @Autowired
  private WxGroupService wxGroupService;
  

    @RequiresPermissions("wxgroup:wxgroup")
	@GetMapping("/index")
	public ModelAndView index() throws WxErrorException{
		ModelAndView mView=new ModelAndView(prefix+"index");
		return mView;
	}
  
  @RequiresPermissions("wxgroup:wxgroup")
  @RequestMapping("/list")
  public PageModel list(@RequestBody WxGroup wxGroup) throws WxErrorException {
	  List<WxGroup> groups=wxGroupService.selectByMap(new HashMap<>());
	  if(groups==null||groups.isEmpty()){
		  List<WxUserTag> list= this.wxMpService.getUserTagService().tagGet();
		  wxGroupService.initGroup(list);
	  }
	  Query<WxGroup> query = new Query<WxGroup>(wxGroup,false);
	  List<WxGroup> wxGroups=wxGroupService.selectList(query.getEntityWrapper());
	  PageModel pa=new PageModel(wxGroups,wxGroups.size());
      return pa;
  }
  
  @RequiresPermissions("wxgroup:del")
  @RequestMapping("/deleteRow/{tagId}")
  public R deleteRow(@PathVariable("tagId") Long tagId) throws WxErrorException {
	  Boolean res= this.wxMpService.getUserTagService().tagDelete(tagId);
	  if(res){
		  wxGroupService.deleteById(tagId);
		  return R.ok();
	  }
	  else{
		  return R.error();
	  }
  }
  
    @RequiresPermissions("wxgroup:del")
	@PostMapping("/deleteRows")
	public R deleteRows(@RequestParam(value="ids[]") List<Long> ids) throws WxErrorException{
		if(ids!=null){
			for(Long id:ids){
				this.wxMpService.getUserTagService().tagDelete(id);
			}
			wxGroupService.deleteByIds(ids);
			return R.ok();
		}else{
			 return R.error();
		}
	}
  
  
    @RequiresPermissions("wxgroup:add")
	@GetMapping("/add")
	public ModelAndView add(){
		ModelAndView mView=new ModelAndView(prefix+"add");
		return mView;
	}
  
    @RequiresPermissions("wxgroup:edit")
	@GetMapping("/edit")
	public ModelAndView edit(@RequestParam("gid") Long gid,@RequestParam("gname") String gname){
		ModelAndView mView=new ModelAndView(prefix+"edit");
		mView.addObject("gid",gid );
		mView.addObject("gname",gname );
		return mView;
	}
	
    @RequiresPermissions("wxgroup:edit")
	@PostMapping("/update")
	public R edit(@RequestBody WxUserTag wxUserTag) throws WxErrorException{
		  Boolean res= this.wxMpService.getUserTagService().tagUpdate(wxUserTag.getId(), wxUserTag.getName());
		  if(res){
			  WxGroup wxGroup=new WxGroup();
			  wxGroup.setName(wxUserTag.getName());
			  wxGroup.setId(wxUserTag.getId());
			  wxGroupService.update(wxGroup);
			  return R.ok();
		  }
		  else{
			  return R.error(); 
		  }
	}
	
    @RequiresPermissions("wxgroup:add")
	@PostMapping("/save")
	public R save(@RequestBody WxUserTag wxUserTag) throws WxErrorException{
		  WxUserTag res= this.wxMpService.getUserTagService().tagCreate(wxUserTag.getName());
		  if(res!=null){
			  WxGroup wxGroup=new WxGroup();
			  try {
				BeanUtils.copyProperties(wxGroup, res);
			  } catch (IllegalAccessException | InvocationTargetException e) {
				e.printStackTrace();
			  }
			  wxGroupService.save(wxGroup);
			  return R.ok();
		  }
		  else
		  {
			  return R.error();
		  }
	}
    
    @RequestMapping("selectTreeList")
	public Tree<WxGroup> selectTreeList(){
		Tree<WxGroup> groups=wxGroupService.selectTreeList();
		return groups;
	}
	
}
